package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.EmploymentEntity;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.kjwl.model.entity.FindPeopleEntity;
import com.sgb.kjwl.model.entity.PopularWorkersEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.model.entity.packers.LabourServiceAndEmploymentEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author : younghow
 * @date : 2021/8/18 15:00
 * description:工队首页下半部分
 */
public class PackerChangeSecondViewModel extends AndroidViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<List<LabourServiceAndEmploymentEntity>> mFollowSuccessLiveData = new MutableLiveData<>();
    public MutableLiveData<List<FindPackersEntity>> mFollowTeamLiveData = new MutableLiveData<>();
    public MutableLiveData<List<FindPeopleEntity>> mFollowWorkerLiveData = new MutableLiveData<>();
    public MutableLiveData<List<EmploymentEntity>> mFollowTemporaryEmploymentLiveData = new MutableLiveData<>();
    public MutableLiveData<List<TenderListEntity>> mSuccessFindBuildLiveData = new MutableLiveData<>();
    public MutableLiveData<List<PopularWorkersEntity>> mSuccessPopularLiveData = new MutableLiveData<>();
    public MutableLiveData<String> mUrlLiveData = new MutableLiveData<>();
    public MutableLiveData<String> mUrlTenderLiveData = new MutableLiveData<>();

    public PackerChangeSecondViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void signUpTerm(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getLabourList(map).enqueue(new Callback<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Response<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowSuccessLiveData.postValue(response.body().getData().getList());
                }else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getTeamList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeamList(map).enqueue(new Callback<BaseEntity<ListResponse<List<FindPackersEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Response<BaseEntity<ListResponse<List<FindPackersEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowTeamLiveData.postValue(response.body().getData().getList());
                }else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getWorkerList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPackerPeopleList(map).enqueue(new Callback<BaseEntity<ListResponse<List<FindPeopleEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<FindPeopleEntity>>>> call, Response<BaseEntity<ListResponse<List<FindPeopleEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowWorkerLiveData.postValue(response.body().getData().getList());
                }else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<FindPeopleEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     * 获取临时用工列表
     * @param map 参数 详情见API注释
     */
    public void getTemporaryEmploymentList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getEmploymentList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EmploymentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EmploymentEntity>>>> call, Response<BaseEntity<ListResponse<List<EmploymentEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowTemporaryEmploymentLiveData.postValue(response.body().getData().getList());
                }else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EmploymentEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     * 工队详情H5链接
     *
     * @param id 当前工队的no
     */
    public void getH5Url(String id) {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPickAPI().getUrlSetting("teamWorkDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + id + "&type=0";
                    MLog.d("api", "H5跳转链接  " + url);
                    mUrlLiveData.postValue(url + "----" + id);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     * 首页找工程
     * @param map
     */
    public void getTenderApplyingList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTenderApplyingList(map).enqueue(new Callback<BaseEntity<List<TenderListEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TenderListEntity>>> call, Response<BaseEntity<List<TenderListEntity>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mSuccessFindBuildLiveData.postValue(response.body().getData());
                }else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TenderListEntity>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     * 人气工队(首页)
     * @param map 参数
     */
    public void getPopularList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPopularWorkers(map).enqueue(new Callback<BaseEntity<ListResponse<List<PopularWorkersEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PopularWorkersEntity>>>> call, Response<BaseEntity<ListResponse<List<PopularWorkersEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mSuccessPopularLiveData.postValue(response.body().getData().getList());
                }else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PopularWorkersEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     * 班组工队（施工队）详情h5链接请求
     * @param no 这一条的no
     */
    public void teamParticulars(String no) {
        APIManager.getInstance().getPickAPI().getUrlSetting("teamWorkDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    //参数1:type(0:	前台,1:用户中心);	参数2:no
                    String url = data.getData().getUrl() + "?no=" + no + "&type=0";
                    mUrlLiveData.postValue(url + "----" + no);
                }
            }
        });
    }

    public void getUrlSettingTender(String id) {
        APIManager.getInstance().getPickAPI().getUrlSetting("FrontDeskBidding").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    mUrlTenderLiveData.postValue(url + "----" + id);
                }
            }
        });
    }
}
